package org.jeecg.modules.wlhy.hycarrier.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.wlhy.hycarrier.entity.HyCarrierAccountBook;
import org.jeecg.modules.wlhy.hycarrier.mapper.HyCarrierAccountBookMapper;
import org.jeecg.modules.wlhy.hycarrier.service.IHyCarrierAccountBookService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @Description: hy_carrier_account_book
 * @Author: jeecg-boot
 * @Date:   2022-08-05
 * @Version: V1.0
 */
@Service
public class HyCarrierAccountBookServiceImpl extends ServiceImpl<HyCarrierAccountBookMapper, HyCarrierAccountBook> implements IHyCarrierAccountBookService {

    @Override
    public HyCarrierAccountBook getAccountBookByCarrierId(String carrierId) {
        return this.getOne(
                new QueryWrapper<HyCarrierAccountBook>().eq("carrier_id", carrierId)
        );
    }

    @Override
    public String createLogAccNo() {
        for (int i = 0; i < 10; i++) {
            String logAccNo =  "5" + RandomUtil.randomNumbers(9);
            int count = this.count(new QueryWrapper<HyCarrierAccountBook>().eq("log_acc_no",logAccNo));
            if(count == 0) {
                return logAccNo;
            }
        }
        throw new JeecgBootException("生成失败，请重试");
    }

    @Override
    public List<HyCarrierAccountBook> ListByCarrierIds(Set<String> carrierSet) {
        List<HyCarrierAccountBook> accountBooks = this.list(
                new QueryWrapper<HyCarrierAccountBook>().in("carrier_id", carrierSet)
        );
        return accountBooks;
    }
}
